/*
 *Create By：Z.B
 *@DATE 2019/3/19 15:04
 *@EMAIL: 2545375@qq.com
 */
'use strict';

const path = require('path');
const Controller = require('egg').Controller;

class UserAccessController extends Controller{

  constructor(ctx){
    super(ctx)

    this.UserLoginTransfer = {
      mobile: { type: 'string', required: true, allowEmpty: false },
      password: { type: 'string', required: true, allowEmpty: false }
    };

    this.UserResetPswTransfer = {
      password: { type: 'password', required: true, allowEmpty: false, min: 6 },
      oldPassword: { type: 'password', required: true, allowEmpty: false, min: 6 }
    };

    this.UserUpdateTransfer = {
      mobile: { type: 'string', required: true, allowEmpty: false },
      realName: {type: 'string', required: true, allowEmpty: false, format: /^[\u2E80-\u9FFF]{2,6}$/}
    };
  }

  async login(){
    const { ctx, service } = this;

    ctx.validate(this.UserLoginTransfer);

    const payload = ctx.request.body || {};
    const res = await service.userAccess.login(payload);
    ctx.helper.success({ctx, res});
  }

  async current(){
    const { ctx, service } = this
    const res = await service.userAccess.current();
    // 设置响应内容和响应状态码
    ctx.helper.success({ctx, res})
  }

  async resetSelf(){
    const {ctx, service} = this;
    ctx.validate(this.UserUpdateTransfer);
    //组装参数
    const payload = ctx.request.body || {};
    await service.userAccess.resetSelf(payload);
    ctx.helper.success({ctx});
  }
}

module.exports = UserAccessController;